package ${bussPackage}.${entityPackage}.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chrhc.projects.cpub.back.common.utils.UtilString;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.github.pagehelper.Page;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;

import com.chrhc.projects.cpub.back.common.utils.Result;
import com.chrhc.projects.cpub.back.common.utils.UtilResult;
//import com.chrhc.projects.cpub.back.common.Page;
import com.chrhc.projects.cpub.back.common.Constants;
import com.chrhc.projects.framework.web.UtilServlet;

import ${bussPackage}.${entityPackage}.model.${className};
import ${bussPackage}.${entityPackage}.service.${className}ServiceI;

import com.chrhc.projects.uiap.common.persistence.entity.CurrentUser;
import com.chrhc.projects.uiap.common.utils.ShiroUtils;
 
/**
 * 
 * <br>
 * <b>功能：</b>${className}Controller<br>
 * <b>作者：</b>${author}<br>
 * <b>日期：</b>${curDate?string("yyyy-MM-dd")} <br>
 * <b>版权所有：<b>版权所有(C) chrhc<br>
 */ 
@Controller
@RequestMapping("/${entityPackage}/${leafEntityPackage}") 
public class ${className}Controller {
	
	private final static Logger log= Logger.getLogger(${className}Controller.class);
	
	// Servrice start
	@Autowired(required=false) //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private ${className}ServiceI ${lowerName}Service; 
	
	
	/**
	 * 初始化主页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "index",method = RequestMethod.GET)
	@ResponseBody
	public Result index(ServletRequest request){

		return UtilResult.create();
	}
	
	
	/**
	 * 分页查询
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "list",method = RequestMethod.POST)
	@ResponseBody
	public Result list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "pagesize", defaultValue = Constants.PAGE_SIZE+"") int pageSize,
			@RequestParam(value = "sortType", defaultValue = "update_time") String sortType, 
			Model model, ServletRequest request){
		Map<String, Object> searchParams = UtilServlet.getParametersStartingWith(request, "search_");
		com.chrhc.projects.cpub.back.common.CpubPage<${className}> page = ${lowerName}Service.findByPage(searchParams, pageNumber,pageSize,sortType);
		return UtilResult.create(page);
	}
	
	
	/**
	 * 根据查询条件获取符合要求的组织机构(不分页)
	 * @param request
	 * @return
	 */
	@RequestMapping(value="all" ,method = RequestMethod.POST)
	@ResponseBody
	public Result findAll(ServletRequest request){
		Map<String, Object> searchParams = UtilServlet.getParametersStartingWith(request, "search_");
		List<${className}> list = ${lowerName}Service.findAll(searchParams);
		return UtilResult.create(list);
	}
	
	
	/**
	 * 根据Id取得角色对象
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "get/{id}",method = RequestMethod.GET)
	@ResponseBody
	public Result get(@PathVariable("id") String id){
		${className} vo = ${lowerName}Service.get(id);
		return UtilResult.create(vo);
	}
	

	/**
	 * 保存角色
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="save",method = RequestMethod.POST)
	@ResponseBody
	public Result save(${className} entity){
		CurrentUser user = ShiroUtils.getCurrentUser();
		${lowerName}Service.save(entity,user);
		return UtilResult.create(Constants.MESSAGE_PUBLIC_SAVE);
	}
	
	/**
	 * 删除id
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="delete/{id}" ,method = RequestMethod.GET)
	@ResponseBody
	public Result delete(@PathVariable("id") String id,Model model){
		${lowerName}Service.delete(id);
		return UtilResult.create(Constants.MESSAGE_PUBLIC_DELETE);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @param model
	 * @return
	 */
	@RequestMapping(value="deleteByIds/{ids}" ,method = RequestMethod.GET)
	@ResponseBody
	public Result deleteByIds(@PathVariable("ids") String ids){
		if (ids != null) {
			String[] idArr = ids.split(",");
			${lowerName}Service.deleteByIds(idArr);
		}
		return UtilResult.create(Constants.MESSAGE_PUBLIC_DELETE);
	}
	
	
	
	
	
	

}
